package com.ruoyi.littleRedBook.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.littleRedBook.domain.pojo.Reservation;
import com.ruoyi.littleRedBook.mapper.ReservationMapper;
import com.ruoyi.littleRedBook.service.ReservationService;
import com.ruoyi.littleRedBook.tools.SmsCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * <p>
 * 预约表 服务实现类
 * </p>
 *
 * @author
 * @since 2025-09-10
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {

    @Resource
    JavaMailSender mailSender;


    @Override
    public String sendMails() {

        // Do the business calculations...
        // Call the collaborators to persist the order...
        String code = SmsCodeUtils.generateNumericCode(6);



        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                //接受邮箱地址
                mimeMessage.setRecipient(Message.RecipientType.TO,
                        new InternetAddress("2042386965@qq.com"));
                //发送邮箱地址
                mimeMessage.setFrom(new InternetAddress("2042386965@qq.com"));
                //邮箱内容
                mimeMessage.setText("您的验证码为" + code );
            }
        };

        try {
            this.mailSender.send(preparator);
            return code;
        }
        catch (MailException ex) {
            // simply log it and go on...
            System.err.println(ex.getMessage());
            return null;
        }
    }
}
